import {
  getAllTags,
  getUserRandom,
  getUsers_base_info,
  getUserTags,
  stickUserTag,
  getEmployeeInfo,
} from '../services/tag'

export default {
  namespace: 'tag',
  state: {
    users: [],
    tags: [],
    filterUsers: [],
    personalTags: {},
    info: {},
    flag:true
  },

  effects: {
    *stickTag({ payload }, { call, put }) {
      yield call(stickUserTag, payload);
    },
    *getEmployeeInfo({ payload }, { call, put }) {
      const response = yield call(getEmployeeInfo, payload)
      yield put({
        type: 'getInfo',
        payload: response,
      });
    },
    *getPersonalTags({ payload }, { call, put }) {
      const response = yield call(getUserTags, payload);
      yield put({
        type: 'setPersonalTags',
        payload: response
      })
    },
    *filterUser({ payload }, { call, put }) {
      const response = yield call(getUsers_base_info, payload);
      yield put({
        type: 'setFilterUser',
        payload: response
      })
    },
    *getUserList({ payload }, { call, put }) {
      const response = yield call(getUserRandom, payload);
      yield put({
        type: 'getUsers',
        payload: response
      })
    },
    *getAllTags({ payload }, { call, put }) {
      const response = yield call(getAllTags, payload);
      yield put({
        type: 'getTags',
        payload: response
      })
    },
  },

  reducers: {
    getInfo(state, { payload }) {
      return {
        ...state,
        info: payload,
      }
    },
    setPersonalTags(state, { payload }) {
      return {
        ...state,
        flag:false,
        personalTags: payload
      }
    },
    clearFilterUser(state) {
      return {
        ...state,
        filterUsers: []
      }
    },
    setFilterUser(state, { payload }) {
      return {
        ...state,
        filterUsers: [].concat(payload)
      }
    },
    getUsers(state, { payload }) {
      return {
        ...state,
        users: [].concat(payload)
      }
    },
    getTags(state, { payload }) {
      return {
        ...state,
        tags: [].concat(payload)
      }
    },
  },
};
